﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace KPI.Utility
{
    public class DatabaseConnectionProvider
    {
        private const string KPIDatabaseConnectionStringKey = "KPIDatabase";

        /// <summary>
        /// Gets the SQL server connection.
        /// </summary>
        /// <param name="connectionStringKey">The connection string key.</param>
        /// <returns></returns>
        public static SqlConnection GetSqlServerConnection(string connectionStringKey)
        {
            string connectionString = ConfigurationManager.ConnectionStrings[connectionStringKey].ConnectionString;
            if (string.Empty.Equals(connectionString))
                throw new ConfigurationErrorsException(string.Format("Can not find connection string key: {0}", connectionStringKey));

            return new SqlConnection(connectionString);
        }

        /// <summary>
        /// Gets the KPI SQL Server database connection.
        /// </summary>
        /// <returns></returns>
        public static SqlConnection GetKPISqlDbConnection()
        {
            return DatabaseConnectionProvider.GetSqlServerConnection(DatabaseConnectionProvider.KPIDatabaseConnectionStringKey);
        }
    }
}
